package com.wx.web.controller.course;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.util.StringUtil;
import com.wx.business.course.IxbBusinessPictureService;
import com.wx.business.course.IxbCourseCategoryService;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.business.course.IxbCourseShareService;
import com.wx.business.course.IxbCourseVisitService;
import com.wx.business.course.IxbTrainingAgencyService;
import com.wx.business.util.IsActivity;
import com.wx.business.util.IsFinish;
import com.wx.business.util.IsOnline;
import com.wx.business.util.XMathUtil;
import com.wx.dao.mapper.IxbCourseVisitMapper;
import com.wx.model.AppConfig;
import com.wx.model.IxbCourseDetailExample;
import com.wx.model.IxbCourseVisitExample;
import com.wx.model.IxbTrainingAgency;

/**
 * 课程管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-08 18:27:50
 */
@Controller
@RequestMapping("/ixbCourseDetail")
public class IxbCourseDetailController {


    @Autowired
    private IxbCourseDetailService ixbCourseDetailService;

    @Autowired
    private IxbCourseCategoryService ixbCourseCategoryService;
    
    
    @Autowired
    private IxbTrainingAgencyService ixbTrainingAgencyService;
    
    @Autowired
    private IxbCourseVisitService ixbCourseVisitService;
    
    @Autowired
    private IxbCourseVisitMapper ixbCourseVisitMapper;
    
    @Autowired
    private IxbCourseShareService ixbCourseShareService;

    @Autowired
    private IxbBusinessPictureService iIxbBusinessPictureService;

    /**
     *  课程管理详情
     * @param ixbCourseDetailId
     * @param userId
     * @param shareUserId
     * @return
     */
    @RequestMapping(value = "/h5/detail")
    @ResponseBody
    public JsonResult h5detail(String ixbCourseDetailId,String userId,String shareUserId) {
    
    	if(StringUtils.isEmpty(ixbCourseDetailId))
    		return null;
    	
    	
    	try {
			ixbCourseVisitService.visitCourse(ixbCourseDetailId, userId, shareUserId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	IxbCourseVisitExample  courseVisitExt=new IxbCourseVisitExample();
    	courseVisitExt.setCourseId(ixbCourseDetailId);
    	//访问用户
    	List<Map<String,Object>> visitUsers=ixbCourseVisitMapper.queryCourseVisiterByCourseId(courseVisitExt);
    	
    	
    	//购买用户
    	List<Map<String,Object>> buyers=ixbCourseDetailService.queryCourseBuyers(ixbCourseDetailId);

    	//课程详情
    	Map<String,Object> detail =ixbCourseDetailService.queryCourseDetailById(ixbCourseDetailId);;
    	
    	IxbTrainingAgency  agencyInfo=ixbTrainingAgencyService.selectByPrimaryKey(detail.get("publisherId")+"");
    	detail.put("businessPicUrl", AppConfig.BaseUrl+detail.get("businessPic"));
    	detail.put("businessPic", detail.get("businessPic"));
    	detail.put("expPrice",XMathUtil.multiply(detail.get("coursePrice")+"", detail.get("discount")+"") );
    	Date endTime=(Date) detail.get("endTime");
    	long endTimelong=endTime.getTime()-System.currentTimeMillis();
    	detail.put("endTimelong", endTimelong);
    	Map<String,Object> dataMap=new HashMap<String, Object>();
    	dataMap.put("visitUsers", visitUsers);
    	dataMap.put("buyers", buyers);
    	dataMap.put("agencyInfo", agencyInfo);
    	dataMap.put("courseDetail", detail);
        return new JsonResult(true, dataMap);
    }

    
    /**
     * 获取课程管理列表
     * @param condition
     * @param courseType
     * @param start
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "h5/list")
    @ResponseBody
    public JsonResult h5list(String condition,String courseType,int start,int pageSize) {
    	Pagination<Map<String,Object>>  page=new Pagination<Map<String,Object>>(start, pageSize);
    	IxbCourseDetailExample  courseWrapper=new IxbCourseDetailExample();
    	courseWrapper.setAsc(false);
    	
    	if(StringUtil.isNotEmpty(condition))
    	{
    		courseWrapper.setCourseTitle(condition);
    	}
    	
    	if(StringUtil.isNotEmpty(courseType))
    	{
    		courseWrapper.setCourseType(courseType);
    	}
    	
    	courseWrapper.setPagination(page);
    	List<Map<String,Object>>  pageList=ixbCourseDetailService.selectMapsByPage(courseWrapper);
    	for (Map<String,Object> map:pageList) {
            map.put("businessPic", AppConfig.BaseUrl+map.get("businessPic"));
            map.put("expPrice",XMathUtil.multiply(map.get("coursePrice")+"", map.get("discount")+"") );
        	Date endTime=(Date) map.get("endTime");
        	long endTimelong=endTime.getTime()-System.currentTimeMillis();
        	map.put("endTimelong", endTimelong);
		}
    	page.setDataList(pageList);
        return new JsonResult(true,page) ;
    }
    
    

    /**
     * 获取推荐课程管理列表
     * @param request
     * @param condition
     * @return
     */
    @RequestMapping(value = "h5/reclist")
    @ResponseBody
    public JsonResult h5reclist(HttpServletRequest request,String condition) {
    	
    	List<Map<String,Object>>  courselist=	ixbCourseDetailService.selectRecCourseList(condition);
    	
    	for (Map<String,Object> map:courselist) {
    		 map.put("isFinishName", IsFinish.getValue( map.get("isFinish")+""));
             map.put("isActivityName",IsActivity.getValue( map.get("isActivity")+""));
             map.put("isOnlineName", IsOnline.getValue( map.get("isOnline")+""));
             map.put("levelOneCourseTypeName",ixbCourseCategoryService.getCourseCategoryName(map.get("levelOneCourseType")+""));
             map.put("levelTwoCourseTypeName", ixbCourseCategoryService.getCourseCategoryName(map.get("levelTwoCourseType")+""));
             map.put("businessPic", AppConfig.BaseUrl+map.get("businessPic"));
		}
          
        return new JsonResult(true, courselist) ;
    }
}
